package com.example.blog.service.Impl;

import cn.hutool.core.io.IoUtil;
import com.example.blog.service.FileService;
import com.example.blog.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: lu
 * @Date: 2023-11-23-16:39
 * @Description:
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Value("${file.upload.url}")
    private  String uploadFilePath;

    Date date = new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public JsonResult uploadFile(MultipartFile multipartFile) {

        //文件保存路径
        //String filePath = "E://file";
        File filePath = new File(uploadFilePath);
        //如果文件夹不存在则创建
        if (!filePath.exists() && !filePath.isDirectory()) {
            filePath.mkdirs();
        }
        String fileName = multipartFile.getOriginalFilename();
        File file = new File(filePath + File.separator + fileName);

        //将输入流中数据复制到输出流中
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            IoUtil.copy(multipartFile.getInputStream(), fileOutputStream);
            log.info("file upload success!");
        } catch (FileNotFoundException e) {
            log.error("FileNotFoundException", e);
            //e.printStackTrace();
        } catch (IOException e) {
            log.error("IOException", e);
            //e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                log.error("IOException1", e);
            }
        }
        return JsonResult.success1(file, 200, simpleDateFormat.format(date));

    }

    @Override
    public void downloadFile(String name, HttpServletResponse response) {
        File file = new File(uploadFilePath + File.separator + name);

        if (!file.exists()) {
            log.info("文件不存在");
        }
        response.setContentType("application/force-download");
        response.addHeader("Content-Disposition", "attachment;fileName=" + name);

        byte[] buffer = new byte[1024];

        try {
            try (FileInputStream fis = new FileInputStream(file);
                 BufferedInputStream bis = new BufferedInputStream(fis)) {

                OutputStream os = response.getOutputStream();

                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }

                log.info("文件下载成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public JsonResult uploads(HttpServletRequest request) {
        List<MultipartFile> files  = ((MultipartHttpServletRequest) request).getFiles("uploadFile");

        for(MultipartFile file : files){
            uploadFile(file);
        }

        return JsonResult.success1(files,200,simpleDateFormat.format(date));
    }

    //@Override
    //public JsonResult uploadFiles(MultipartFile[] files) {
    //
    //    return null;
    //}
}
